package com.gm.wholesale.module.custom.ui;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.custom.ChoiceCustomData;
import com.gm.wholesale.module.custom.api.ICustomApi;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.bean.DefaultCustomBean;
import com.gm.wholesale.module.custom.model.CustomModel;
import com.gm.wholesale.module.custom.view.CustomView;

import java.util.HashMap;

import io.reactivex.Observable;


public class ChoiceCustomFragment extends BaseGridFragment<CustomBean, BasePageData<CustomBean>> {

    private String keyword;

    public void setKeyword(String keyword) {
        this.keyword = keyword;
        refreshData();
    }

    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        setNumColumns(2);
        setHorizontalSpacing(15);
        setBodyPadding(20);
    }

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        setBodybg(getResources().getColor(R.color.color_DBDEE6));
    }

    private CustomView.onCustomChoiceListener listener;

    public void setOnCustomChoice(CustomView.onCustomChoiceListener listener) {
        this.listener = listener;
    }


    @Override
    public void getDataOnCompleted() {
        super.getDataOnCompleted();
        if (!TextUtils.isEmpty(keyword)) {
            return;
        }
        getDefaultCustom();

    }

    public void getDefaultCustom() {
        CustomModel.getDefaultCustom(null, new BaseHttpObserver<DataResp<DefaultCustomBean>>() {
            @Override
            protected void handleResultOkResp(DataResp<DefaultCustomBean> resp) {
                DefaultCustomBean defaultCustomBean = resp.getData();
                if (defaultCustomBean != null) {
                    CustomBean customBean = new CustomBean();
                    customBean.setName(defaultCustomBean.getName());
                    customBean.setId(defaultCustomBean.getId());
                    mDatas.add(0, customBean);
                    notifyListDataSetChanged();
                }
            }
        }.setAutoErrorToast(false));
    }

    @Override
    protected BaseAdapter createTempBaseAdapter() {
        return new CommonAdapter<CustomBean>(getActivity(), mDatas, R.layout.list_item_custom) {
            @Override
            public void convert(ViewHolder viewHolder, final CustomBean data) {
                viewHolder.setText(R.id.customTv, data.getName());
                viewHolder.getConvertView().setOnClickListener(view -> {
                    Popupwindow.hiddenPop();
                    if (listener != null) {
                        listener.onChoiceCustom(data);
                    }
                    ChoiceCustomData.newInstance().setCustomBean(data);
                });

            }
        };
    }

    @Override
    protected HttpServicesHolder<?, BasePageData<CustomBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<ICustomApi, BasePageData<CustomBean>>() {
            @Override
            public Observable<BasePageData<CustomBean>> getHttpObservable(ICustomApi api) {
                HashMap<String, Object> quearyData = new HashMap<>();
                quearyData.put("page", index);
                if (!TextUtils.isEmpty(keyword)) {
                    quearyData.put("keyword", keyword);
                }
                quearyData.put("pageSize", Constant.DEFAULTPAGESIZE);
                return api.getCustomers(quearyData);
            }
        };
    }


}
